package org.enwey.icarenew.Activity.Personal;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestListResult;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Activity.News.TopicDetailActivity;
import org.enwey.icarenew.Activity.Setting.SettingActivity;
import org.enwey.icarenew.Adapter.UserNewAdapter;
import org.enwey.icarenew.Component.UserNewHeaderView;
import org.enwey.icarenew.Manager.SettingsManager;
import org.enwey.icarenew.Model.Topic;
import org.enwey.icarenew.Model.User;
import org.enwey.icarenew.R;
import java.util.Hashtable;
import java.util.List;


/**
 * Created by enwey on 2014/11/21.
 */
public class UserInfoActivity extends BaseActivity {
    public static final String TAG = "UserInfoActivity";
    private ListView mListView;
    private UserNewAdapter mAdapter;
    private List<Topic> mTopicList;
    private UserNewHeaderView headerView;
    private ImageView mSetting;
    private User mLoginUser;
    private boolean isFirst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);
        setUp();
        findView();
        registerListener();
        CallAPINewList();
    }

    private void setUp() {
        showLeftBtn();
        showRightBtn(R.drawable.ic_setting);
        setTitle("我的主页");
        mLoginUser = SettingsManager.getLoginUser();
        isFirst = true;

        mAdapter = new UserNewAdapter(this);
        headerView = new UserNewHeaderView(this);
    }

    private void findView() {
        mListView = (ListView) this.findViewById(R.id.activity_userinfo_list);
        mSetting = (ImageView) this.findViewById(R.id.header_btn_right);

        mListView.addHeaderView(headerView,null,false);
        mListView.setAdapter(mAdapter);
    }

    private void registerListener() {

        AdapterView.OnItemClickListener onItemRegister = new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(UserInfoActivity.this, TopicDetailActivity.class);
                intent.putExtra("topic_id", mTopicList.get(position - 1).id + "");
                startActivity(intent);
            }
        };
        mListView.setOnItemClickListener(onItemRegister);

        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.header_btn_right) {
                    Intent intent = new Intent(UserInfoActivity.this, SettingActivity.class);
                    startActivity(intent);
                }
            }
        };
        mSetting.setOnClickListener(register);
    }

    private void CallAPINewList() {
        if (isFirst) {
            showProgressDialog();
        }
        Hashtable<String, String> parameters = new Hashtable<>();
        parameters.put("account", mLoginUser.account);
        APIManager.getInstance(this).userNewList(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (isFirst) {
                    hideProgressDialog();
                    isFirst = false;
                }
                showException(error);
            }
        }, new Response.Listener<RequestListResult<Topic>>() {
            @Override
            public void onResponse(RequestListResult<Topic> response) {
                if (isFirst) {
                    hideProgressDialog();
                    isFirst = false;
                }
                if (hasError(response)) {
                    return;
                }

                if (response != null) {
                    if (response.data != null) {
                        mTopicList = response.data;
                        mAdapter.setUserNewList(mTopicList);
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        mLoginUser = SettingsManager.getLoginUser();
        if (headerView != null) {
            headerView.setUserData(mLoginUser);
            headerView.setHeaderImage(mLoginUser);
        }
        CallAPINewList();
    }
}
